import Cookies from 'js-cookie'

/**
 * 小于10补零
 */
export const addZore = num => Number(num) < 10 ? '0' + num : num

/**
 * 判断网络是否正常
 */
export const isOnLine = () => {
    if (navigator.onLine) {
        //网络正常
        return true;
    } else {
        //网络中断
        return false;
    }
}

/**
 *  判断星期几
 */
export const getWeek = (year, month, day) => {
    let weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    return weekDay[new Date(year, month, day)];
}
/**
 * 获取当前月的天数
 */
export const mGetDate = (year, month) => (new Date(year, month, 0)).getDate()

/**
 * 获取url参数值
 */
export const getUrlParam = (
    query = window.location.search.substr(1)
) =>{
    query = query.split('&')
    let params = {}
    for (let i = 0; i < query.length; i++) {
        let q = query[i].split('=')
        if (q.length === 2) {
            params[q[0]] = q[1]
        }
    }
    return params;
}

/**
 * 过滤首尾空格
 */
export const trimStr = str => str.replace(/(^\s*)|(\s*$)/g, "")


/**
 * 判断是否是手机号
 */
export const isPhoneNum = phone => /^[1][3-9][0-9]{9}$/.test(phone)


/**
 * 过滤表情
 */
export const filterEmoji = value => {
    let ranges = ['\ud83c[\udf00-\udfff]', '\ud83d[\udc00-\ude4f]', '\ud83d[\ude80-\udeff]'];
    return value.replace(new RegExp(ranges.join('|'), 'g'), '')
}

/**
 * 过滤字符
 */
export const fliterStr = s => {
    let pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？\"]")
    let rs = "";
    for (var i = 0; i < s.length; i++) {
        rs = rs + s.substr(i, 1).replace(pattern, '');
    }

    rs = rs.replace(/script/ig, '').replace(/\\/g, '');
    return rs
}

/**
 * 判断是否存在特殊字符
 */
export const checkStr = str => {
    let pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
    if (pattern.test(str)) {
        return true;
    }
    return false
}

/**
 * 非数字过滤
 */
export const onlyNum = value => value.replace(/\D/g, '')

/**
 * 毫秒转日期时间
 * @param hs
 * @return {string}
 */
export const hsToTime = hs => {
    let newDate = new Date(Number(hs));
    let year = newDate.getFullYear(); //获取完整的年份(4位,1970-????)
    let month = addZore(newDate.getMonth() + 1); //获取当前月份(0-11,0代表1月)
    let day = addZore(newDate.getDate()); //获取当前日(1-31)
    let hour = addZore(newDate.getHours()); //获取当前小时数(0-23)
    let minute = addZore(newDate.getMinutes()); //获取当前分钟数(0-59)
    let second = addZore(newDate.getSeconds()); //获取当前秒数(0-59)

    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

/**
 * 获取当前日期
 * @param str
 */
export const getDate = (str = '-') => {
    let newDate = new Date();

    //日期
    let year = newDate.getFullYear(); //获取完整的年份(4位,1970-????)
    let month = addZore(newDate.getMonth() + 1); //获取当前月份(0-11,0代表1月)
    let day = addZore(newDate.getDate()); //获取当前日(1-31)

    return [year, month, day].join(str);
}

/**
 * 获取当前日期时间
 * @return {string}
 */
export const nowHsToTime = (str = '-') => {
    let newDate = new Date();

    let hour = addZore(newDate.getHours()); //获取当前小时数(0-23)
    let minute = addZore(newDate.getMinutes()); //获取当前分钟数(0-59)
    let second = addZore(newDate.getSeconds()); //获取当前秒数(0-59)

    return `${getDate(str)} ${[hour, minute, second].join(':')}`;
}


/**
 * 判断页面是否被嵌入iframe里面
 * @return {boolean} 返回布尔值
 *      如果返回true –> 说明页面被嵌套在iframe中了
 *      如果返回false –> 说明页面并没有被嵌套在iframe中
 */
export const isIframe = () => window.self !== window.top

/**
 * 数组去重
 * @param arr
 * @return {Array}
 */
export const removeArr = arr => {
    let temp = {};
    let newArr = [];

    for (let i = 0; i < arr.length; i++) {
        if (!temp[arr[i]]) {
            temp[arr[i]] = true;
            newArr.push(arr[i]);
        }
    }
    return newArr;
}

/***
 * 获取屏幕工作区高度
 * @param subHeight 减去的高度
 * @return {number}
 */
export const getHeight = (subHeight=0)=>{
    return window.innerHeight-subHeight
}


/**
 * 判断是否是PC端
 * @returns {boolean}
 */
export const isPC = ()=>document.documentElement.clientWidth>900